package service

import (
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"smart-sensor-cloud/pkg/auth"
	"smart-sensor-cloud/pkg/middleware"
	"smart-sensor-cloud/pkg/util"
)

type (
	CaptchaCfg struct {
		KeyLong   int `json:"keyLong"`
		ImgWidth  int `json:"imgWidth"`
		ImgHeight int `json:"imgHeight"`
	}
	CaptchaResponse struct {
		CaptchaId string `json:"captchaId"`
		PicPath   string `json:"picPath"`
	}
)

var captchaCfg = CaptchaCfg{
	KeyLong:   6,
	ImgHeight: 80,
	ImgWidth:  240,
}
var store = base64Captcha.DefaultMemStore

// @Tags base
// @Summary 生成验证码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /base/captcha [post]
func Captcha(ctx *gin.Context) {
	//字符,公式,验证码配置
	// 生成默认数字的driver
	driver := base64Captcha.NewDriverDigit(captchaCfg.ImgHeight, captchaCfg.ImgWidth, captchaCfg.KeyLong, 0.7, 80)
	cp := base64Captcha.NewCaptcha(driver, store)
	id, b64s, err := cp.Generate()
	if err != nil {
		ctx.Set("error", err.Error())
	} else {
		response := &CaptchaResponse{
			CaptchaId: id,
			PicPath:   b64s,
		}

		util.ResponseOK(ctx, util.OKWithPayload(response))
	}
}

func JsonInBlacklist(ctx *gin.Context) {

	jwtGenerator := ctx.MustGet(middleware.JWTGeneratorKey).(*auth.JWTGenerator)

	token := GetToken(ctx)

	err := jwtGenerator.DestroyToken(token)
	if err != nil {
		//util.ResponseOK(ctx, util.OKWithPayload())
	} else {
		//util.OK()
		util.ResponseOK(ctx, util.OKWithPayload(nil))
	}
}
